#!/bin/bash
{
	#////////////////////////////////////
	# DietPi Function:
	# - Enables control and applies settings for specific software and configs.
	#
	#////////////////////////////////////
	# Created by Daniel Knight / daniel.knight@dietpi.com / dietpi.com
	#
	#////////////////////////////////////
	#
	# Usage:
	readonly FP_SCRIPT='/boot/dietpi/func/dietpi-set_software'
	readonly AVAIABLE_COMMANDS="
Available commands:
$FP_SCRIPT	locale			xx_XX.UTF-8 (eg: en_GB.UTF-8)
$FP_SCRIPT	allo			eth_dhcp|eth_static [<address>] [<gateway>] [<netmask>] [<dns-nameservers>]
$FP_SCRIPT	useradd			X=create user with name X and default permissions, using global DietPi password (dietpi)
$FP_SCRIPT	userdel			X=delete user with name X
$FP_SCRIPT	apt-mirror		<url>|default
$FP_SCRIPT	apt-cache		clean|reset|cache|lists|archives [disable|ram|disk]
$FP_SCRIPT	ntpd-mode		[0-4] Time sync modes: 0=custom|1=boot|2=boot+daily|3=boot+hourly|4=daemon+drift
$FP_SCRIPT	verify_dietpi.txt	Verifies dietpi.txt entries, adds missing entries if required
$FP_SCRIPT	passwords		NULL=Prompt user to change DietPi related passwords | X=optional set X as global password for future dietpi-software installations and \"root\" + \"dietpi\" login passwords.
"	#////////////////////////////////////

	# Grab inputs
	INPUT_MODE_NAME=$1
	INPUT_MODE_VALUE=$2

	INPUT_ADDITIONAL_1=$3
	INPUT_ADDITIONAL_2=$4
	INPUT_ADDITIONAL_3=$5
	INPUT_ADDITIONAL_4=$6

	# Import DietPi-Globals --------------------------------------------------------------
	. /boot/dietpi/func/dietpi-globals
	readonly G_PROGRAM_NAME='DietPi-Set_software'
	G_CHECK_ROOT_USER
	G_CHECK_ROOTFS_RW
	G_INIT
	# Import DietPi-Globals --------------------------------------------------------------

	EXIT_CODE=0

	Unknown_Input_Name(){

		EXIT_CODE=1
		G_DIETPI-NOTIFY 2 "Unknown input name ($INPUT_MODE_NAME). Nothing has been applied."
		echo "$AVAIABLE_COMMANDS"

	}

	Unknown_Input_Mode(){

		EXIT_CODE=1
		G_DIETPI-NOTIFY 2 "Unknown input value ($INPUT_MODE_VALUE). Nothing has been applied."
		echo "$AVAIABLE_COMMANDS"

	}

	#/////////////////////////////////////////////////////////////////////////////////////
	# locale
	# NB: Adds C.UTF-8 alongside any input option
	#/////////////////////////////////////////////////////////////////////////////////////
	Locale_Main(){

		if [[ $INPUT_MODE_VALUE == 'C.UTF-8' ]] || grep -q "^$INPUT_MODE_VALUE UTF-8$" /usr/share/i18n/SUPPORTED; then

			# Create list of locales to generate
			echo "$INPUT_MODE_VALUE UTF-8" > /etc/locale.gen
			# - Add C.UTF-8 back in, if not chosen, required by DietPi scripts. It's statically pre-compiled, but adding it here adds it to the faster (?) locale cache.
			[[ $INPUT_MODE_VALUE == 'C.UTF-8' ]] || echo 'C.UTF-8 UTF-8' >> /etc/locale.gen

			# Remove current default
			[[ -f '/etc/default/locale' ]] && G_EXEC rm /etc/default/locale

			# Assign new default via debconf: This is set to "None" during noninteractive reconfiguration when the previous default is not compiled anymore.
			debconf-set-selections <<< "locales locales/default_environment_locale select $INPUT_MODE_VALUE"

			# Generate locales and set new default
			# - 1. this calls "locale-gen" to regenerate a fresh locale cache based on /etc/locale.gen to /usr/lib/locale/locale-archive.
			# - 2. this calls "update-locale LANG=<default>" to apply the new default locale form the debconf default assigned above.
			G_CHECK_OUTPUT=1 G_EXEC dpkg-reconfigure -f noninteractive locales

			# Update dietpi.txt entry
			G_CONFIG_INJECT 'AUTO_SETUP_LOCALE=' "AUTO_SETUP_LOCALE=$INPUT_MODE_VALUE" /boot/dietpi.txt

		else

			Unknown_Input_Mode

		fi

	}

	#/////////////////////////////////////////////////////////////////////////////////////
	# Set APT Mirror
	#/////////////////////////////////////////////////////////////////////////////////////
	AptMirror_Main(){

		if [[ $INPUT_MODE_VALUE ]]; then

			# RPi
			if (( $G_HW_MODEL < 10 )); then

				# Component "ui" is in use until Stretch
				local components='main'
				(( $G_DISTRO < 5 )) && components+=' ui'
				# Bullseye is not yet available, revert to Buster
				echo "deb https://archive.raspberrypi.org/debian/ $G_DISTRO_NAME $components" > /etc/apt/sources.list.d/raspi.list

			fi

			# Raspbian
			if (( $G_HW_MODEL < 10 )) && (( $G_RASPBIAN )); then

				# Default?
				[[ $INPUT_MODE_VALUE == 'default' ]] && INPUT_MODE_VALUE='http://raspbian.raspberrypi.org/raspbian/'

				echo "deb $INPUT_MODE_VALUE $G_DISTRO_NAME main contrib non-free" > /etc/apt/sources.list

				# Update dietpi.txt entry
				G_CONFIG_INJECT 'CONFIG_APT_RASPBIAN_MIRROR=' "CONFIG_APT_RASPBIAN_MIRROR=$INPUT_MODE_VALUE" /boot/dietpi.txt

			# Debian
			else

				# Default?
				[[ $INPUT_MODE_VALUE == 'default' ]] && INPUT_MODE_VALUE='https://deb.debian.org/debian/'

				cat << _EOF_ > /etc/apt/sources.list
deb $INPUT_MODE_VALUE $G_DISTRO_NAME main contrib non-free
deb $INPUT_MODE_VALUE $G_DISTRO_NAME-updates main contrib non-free
deb https://deb.debian.org/debian-security/ $G_DISTRO_NAME/updates main contrib non-free
deb $INPUT_MODE_VALUE $G_DISTRO_NAME-backports main contrib non-free
_EOF_

				# On Bullseye, dir structure has changed: https://deb.debian.org/debian-security/dists/
				(( $G_DISTRO > 5 )) && sed -i '3s|/updates|-security|' /etc/apt/sources.list

				# Update dietpi.txt entry
				G_CONFIG_INJECT 'CONFIG_APT_DEBIAN_MIRROR=' "CONFIG_APT_DEBIAN_MIRROR=$INPUT_MODE_VALUE" /boot/dietpi.txt

			fi

		else

			Unknown_Input_Mode

		fi

	}

	#/////////////////////////////////////////////////////////////////////////////////////
	# Set APT Cache
	#/////////////////////////////////////////////////////////////////////////////////////
	AptCache_Main(){

		local fp='/etc/apt/apt.conf.d/99-dietpi-cache'

		# Clean current cache before changing it, failsafe cleanup if custom dir was chosen before
		apt-get clean

		if [[ $INPUT_MODE_VALUE == 'reset' ]]; then

			[[ -f $fp ]] && rm $fp
			[[ -d '/tmp/apt' ]] && rm -R /tmp/apt
			return 0

		fi

		[[ -f $fp ]] || echo '# Please use "dietpi-config" > "Advanded Options" > "APT Cache" to edit these settings.' > $fp

		if [[ $INPUT_MODE_VALUE == 'clean' ]]; then

			rm -Rf /tmp/apt /var/cache/apt /var/lib/apt/{lists,mirrors,periodic,cdroms.list}

		elif [[ $INPUT_MODE_VALUE == 'cache' ]]; then

			if [[ $INPUT_ADDITIONAL_1 == 'disable' ]]; then

				G_CONFIG_INJECT 'Dir::Cache[[:blank:]]' 'Dir::Cache "/tmp/apt";' $fp
				# Leave archives on disk by default
				GCI_PRESERVE=1 G_CONFIG_INJECT 'Dir::Cache::archives[[:blank:]]' 'Dir::Cache::archives "/var/cache/apt/archives";' $fp
				G_CONFIG_INJECT 'Dir::Cache::pkgcache[[:blank:]]' 'Dir::Cache::pkgcache "";' $fp
				G_CONFIG_INJECT 'Dir::Cache::srcpkgcache[[:blank:]]' 'Dir::Cache::srcpkgcache "";' $fp
				[[ -d '/var/cache/apt' ]] && rm -R /var/cache/apt

			elif [[ $INPUT_ADDITIONAL_1 == 'ram' ]]; then

				G_CONFIG_INJECT 'Dir::Cache[[:blank:]]' 'Dir::Cache "/tmp/apt";' $fp
				GCI_PRESERVE=1 G_CONFIG_INJECT 'Dir::Cache::archives[[:blank:]]' 'Dir::Cache::archives "/var/cache/apt/archives";' $fp
				sed -i '/pkgcache[[:blank:]]/d' $fp
				[[ -d '/var/cache/apt' ]] && rm -R /var/cache/apt

			elif [[ $INPUT_ADDITIONAL_1 == 'disk' ]]; then

				sed -i '/^Dir::Cache/d' $fp
				[[ -d '/tmp/apt' ]] && rm -R /tmp/apt

			fi

		elif [[ $INPUT_MODE_VALUE == 'lists' ]]; then

			if [[ $INPUT_ADDITIONAL_1 == 'ram' ]]; then

				G_CONFIG_INJECT 'Dir::State[[:blank:]]' 'Dir::State "/tmp/apt";' $fp
				G_CONFIG_INJECT 'Dir::State::extended_states[[:blank:]]' 'Dir::State::extended_states "/var/lib/apt/extended_states";' $fp
				G_CONFIG_INJECT 'Dir::State::status[[:blank:]]' 'Dir::State::status "/var/lib/dpkg/status";' $fp
				rm -Rf /var/lib/apt/{lists,mirrors,periodic,cdroms.list}

			elif [[ $INPUT_ADDITIONAL_1 == 'disk' ]]; then

				sed -i '/^Dir::State/d' $fp
				[[ -d '/tmp/apt' ]] && rm -R /tmp/apt

			fi

		elif [[ $INPUT_MODE_VALUE == 'archives' ]]; then

			if [[ $INPUT_ADDITIONAL_1 == 'ram' ]]; then

				# Cache needs to be in RAM as well for this to work reliable
				G_CONFIG_INJECT 'Dir::Cache[[:blank:]]' 'Dir::Cache "/tmp/apt";' $fp
				G_CONFIG_INJECT 'Dir::Cache::archives[[:blank:]]' 'Dir::Cache::archives "/tmp/apt/archives";' $fp
				[[ -d '/var/cache/apt' ]] && rm -R /var/cache/apt

			elif [[ $INPUT_ADDITIONAL_1 == 'disk' ]]; then

				G_CONFIG_INJECT 'Dir::Cache::archives[[:blank:]]' 'Dir::Cache::archives "/var/cache/apt/archives";' $fp
				[[ -d '/tmp/apt/archives' ]] && rm -R /tmp/apt/archives

			fi

		fi

		if [[ -f $fp ]]; then

			# Remove drop-in config if it does not contain any setting
			if ! grep -q '^Dir' $fp; then

				rm $fp

			# Re-create default cache dir if archives on disk while cache in RAM, since parent dirs are not auto-created
			elif grep -q '/var/cache/apt/archives' $fp; then

				mkdir -p /var/cache/apt/archives

			fi

		fi

	}

	#/////////////////////////////////////////////////////////////////////////////////////
	# Set time sync mode
	# NB: Also sets mirror as defined in dietpi.txt CONFIG_NTP_MIRROR
	#/////////////////////////////////////////////////////////////////////////////////////
	NtpdMode_Main(){

		if [[ ! $INPUT_MODE_VALUE || $INPUT_MODE_VALUE == [01234] ]]; then

			# If no input given, re-apply current dietpi.txt setting, else revert to default mode 2
			[[ $INPUT_MODE_VALUE ]] || INPUT_MODE_VALUE=$(sed -n '/^[[:blank:]]*CONFIG_NTP_MODE=/{s/^[^=]*=//p;q}' /boot/dietpi.txt)
			[[ $INPUT_MODE_VALUE ]] || INPUT_MODE_VALUE=2

			# Disable temporarily, prevents run_ntpd in dietpi-software
			sed -i '/CONFIG_NTP_MODE=/c\CONFIG_NTP_MODE=0' /boot/dietpi.txt

			local ntp_mirror=$(sed -n '/^[[:blank:]]*CONFIG_NTP_MIRROR=/{s/^[^=]*=//p;q}' /boot/dietpi.txt)
			[[ $ntp_mirror ]] || ntp_mirror='default'

			# Gateway, auto detect local gateway(s)/router(s) to use as NTP server(s).
			if [[ ${ntp_mirror,,} == 'gateway' ]]; then

				# Convert mawk output to single line via ORS, remove trailing space afterwards
				local gateway=$(ip r l 0/0 | mawk '{print $3}' ORS=' '); gateway=${gateway% }
				if [[ $gateway ]]; then

					G_DIETPI-NOTIFY 0 "Gateway(s) detected: $gateway, adding as NTP server(s)."
					ntp_mirror=$gateway

				else

					G_DIETPI-NOTIFY 1 'No local gateway detected. Reverting NTP mirror back to system defaults.'
					EXIT_CODE=1
					ntp_mirror='default'

				fi

			fi

			# Default, lets timesyncd use DHCP server or fallback to debian.pool.ntp.org.
			if [[ ${ntp_mirror,,} == 'default' ]]; then

				[[ -f '/etc/systemd/timesyncd.conf' ]] && G_EXEC rm /etc/systemd/timesyncd.conf

			else

				local i ntp_mirror_entry
				for i in $ntp_mirror
				do
					if [[ $i == *'pool.ntp.org' ]]; then

						ntp_mirror_entry+="0.$i 1.$i 2.$i 3.$i "

					else

						ntp_mirror_entry+="$i "

					fi
				done

				# Set mirror
				echo -e "[Time]\nNTP=${ntp_mirror_entry% }" > /etc/systemd/timesyncd.conf

			fi

			# Daemon mode, dbus required for timedatectl, that users may expect
			if (( $INPUT_MODE_VALUE == 4 )); then

				G_AG_CHECK_INSTALL_PREREQ dbus
				systemctl unmask systemd-timesyncd # Failsafe
				systemctl enable --now systemd-timesyncd

			# Else, disable systemd-timesyncd to let "run_ntpd" or other time sync system take control
			else

				systemctl disable --now systemd-timesyncd

			fi

			# Update dietpi.txt
			G_CONFIG_INJECT 'CONFIG_NTP_MODE=' "CONFIG_NTP_MODE=$INPUT_MODE_VALUE" /boot/dietpi.txt

		else

			Unknown_Input_Mode

		fi

	}

	#/////////////////////////////////////////////////////////////////////////////////////
	# Allo
	#/////////////////////////////////////////////////////////////////////////////////////
	Allo_Main(){

		if [[ $INPUT_MODE_VALUE == 'eth_dhcp' ]]; then

			G_CONFIG_INJECT 'iface eth' 'iface eth0 inet dhcp' /etc/network/interfaces
			sed -i '0,/^.*dns-nameservers[[:blank:]].*$/s//#dns-nameservers 9.9.9.9 149.112.112.112/' /etc/network/interfaces

		elif [[ $INPUT_MODE_VALUE == 'eth_static' ]]; then

			G_CONFIG_INJECT 'iface eth' 'iface eth0 inet static' /etc/network/interfaces
			sed -i "0,\|^.*address[[:blank:]].*\$|s||address $INPUT_ADDITIONAL_1|" /etc/network/interfaces
			sed -i "0,\|^.*gateway[[:blank:]].*\$|s||gateway $INPUT_ADDITIONAL_2|" /etc/network/interfaces
			sed -i "0,\|^.*netmask[[:blank:]].*\$|s||netmask $INPUT_ADDITIONAL_3|" /etc/network/interfaces
			if command -v resolvconf > /dev/null
			then
				sed -i "0,\|^.*dns-nameservers[[:blank:]].*\$|s||dns-nameservers $INPUT_ADDITIONAL_4|" /etc/network/interfaces
			else
				sed -i "0,\|^.*dns-nameservers[[:blank:]].*\$|s||#dns-nameservers $INPUT_ADDITIONAL_4|" /etc/network/interfaces
				> /etc/resolv.conf
				local i
				for i in $INPUT_ADDITIONAL_4; do echo "nameserver $i" >> /etc/resolv.conf; done
			fi

		else

			Unknown_Input_Mode

		fi

	}

	#/////////////////////////////////////////////////////////////////////////////////////
	# Users
	#/////////////////////////////////////////////////////////////////////////////////////
	Useradd_Main(){

		if [[ $INPUT_MODE_VALUE ]]; then

			G_EXEC mkdir -p /home # Failsafe

			# Get global password
			local pbkdf2=()
			(( $G_DISTRO > 4 )) && pbkdf2=('-iter' '10000')
			# - Encrypted
			if [[ -f '/var/lib/dietpi/dietpi-software/.GLOBAL_PW.bin' ]]; then

				GLOBAL_PW=$(openssl enc -d -a -md sha256 -aes-256-cbc "${pbkdf2[@]}" -salt -pass pass:'DietPiRocks!' -in /var/lib/dietpi/dietpi-software/.GLOBAL_PW.bin)

			# - DietPi-PREP, 1st run setup or user cancelled manual password choice
			else

				GLOBAL_PW=$(sed -n '/^[[:blank:]]*AUTO_SETUP_GLOBAL_PASSWORD=/{s/^[^=]*=//p;q}' /boot/dietpi.txt)

			fi
			# - Fallback
			[[ $GLOBAL_PW ]] || GLOBAL_PW='dietpi'

			local usercmd='useradd -m'
			getent passwd "$INPUT_MODE_VALUE" > /dev/null && usercmd='usermod'
			$usercmd "$INPUT_MODE_VALUE" -p "$GLOBAL_PW" -s /bin/bash

			# Copy root profile/bashrc
			[[ -f '/root/.profile' ]] && G_EXEC cp /root/.profile "/home/$INPUT_MODE_VALUE/"
			[[ -f '/root/.bashrc' ]] && G_EXEC cp /root/.bashrc "/home/$INPUT_MODE_VALUE/"

			G_EXEC chown -R "$INPUT_MODE_VALUE:$INPUT_MODE_VALUE" "/home/$INPUT_MODE_VALUE"

			# Allow sudo without pw
			echo "$INPUT_MODE_VALUE ALL=(ALL:ALL) NOPASSWD: ALL" > "/etc/sudoers.d/$INPUT_MODE_VALUE"

		else

			Unknown_Input_Mode

		fi

	}

	Userdel_Main(){

		if [[ $INPUT_MODE_VALUE ]]; then

			# Delete $INPUT_MODE_VALUE
			getent passwd "$INPUT_MODE_VALUE" > /dev/null && G_EXEC userdel -rf "$INPUT_MODE_VALUE"

			# Remove from sudoers
			[[ -f /etc/sudoers.d/$INPUT_MODE_VALUE ]] && G_EXEC rm "/etc/sudoers.d/$INPUT_MODE_VALUE"

		else

			Unknown_Input_Mode

		fi

	}

	Verify_DietPi_Txt(){

		# Pre-v6.29 workaround:
		# - This command is called between DietPi scripts update and incremental patches.
		# - In case of DietPi-Update pre-v6.29, new code is stored to deprecated /DietPi mount point, hence this script loads old DietPi-Globals from /boot/dietpi/.
		# - In older versions, G_GITBRANCH/G_GITOWNER variables are not yet available and further incompatibilities may arise, hence this command is doomed fail.
		# - Additionally in case of Jessie systems, post-v6.25 dietpi.txt entries would be added before the update restarts with v6.25 jessie-support target branch.
		# - Since all pre-v6.29 systems will have DietPi-Update restarted, this command will be called a second time with updated code, so we can skip the faulty first attempt.
		# - Lets use the same check that DietPi-Patch does, to be sure the update restart is done.
		[[ -d '/DietPi' && $(readlink -f '/DietPi') != '/boot' ]] && return 0

		# Add missing entries with dietpi.txt from Git
		G_EXEC_DESC='Downloading current dietpi.txt' G_EXEC curl -sSfL "https://raw.githubusercontent.com/$G_GITOWNER/DietPi/$G_GITBRANCH/dietpi.txt" -o dietpi.txt_patch

		# Remove empty and commented lines
		sed -Ei '/^[[:blank:]]*(#|$)/d' dietpi.txt_patch

		local line entry value
		while read -r line
		do
			entry=${line%%=*} # X=*
			value=${line#*=}  # *=X

			# Entry exists already, continue with next line
			grep -q "^[[:blank:]]*$entry=" /boot/dietpi.txt && continue

			# Entry does not exist yet, add it transparently via G_CONFIG_INJECT
			G_CONFIG_INJECT "$entry=" "$entry=$value" /boot/dietpi.txt

		done < dietpi.txt_patch
		rm dietpi.txt_patch

	}

	Passwords_Main(){

		local pw_dietpi_software pw_root_dietpi_users

		# Automation/input mode, set and then apply input password to both dietpi-software and root/dietpi user passwords.
		if [[ $INPUT_MODE_VALUE ]]; then

			pw_dietpi_software=$INPUT_MODE_VALUE
			pw_root_dietpi_users=$INPUT_MODE_VALUE

		# Prompt to change
		else

			# DietPi-Software PW
			if G_WHIP_YESNO 'Do you want to adjust the default global software password for DietPi-Software installations? We especially recommend to change the default password "dietpi". This does not affect any existing software passwords.\n
NB: This password will be saved and encrypted within "/var/lib/dietpi/dietpi-software/.GLOBAL_PW.bin" to be useable by DietPi scripts for e.g. web application and database logins. We highly recommend to apply individual passwords for each software title after first login.'; then

				G_WHIP_PASSWORD "Please enter a new global software password:\nNB: The following characters are not supported \$|\\\"'"
				pw_dietpi_software=$result
				unset result

			fi

			# Root/DietPi user PW
			if G_WHIP_YESNO 'Change existing unix user passwords?\n\nDietPi has two accounts by default "root" and "dietpi". On first boot, both share the global password "dietpi", respectively the one set in "dietpi.txt".\n
It is highly recommended to change this password, ideally, it should be different than the global software password.\n\nWould you like to change the login passwords for "root" and "dietpi"?'; then

				G_WHIP_PASSWORD 'Please enter a new unix user password:'
				pw_root_dietpi_users=$result
				unset result

			fi

		fi

		# Apply | dietpi-software PW
		if [[ $pw_dietpi_software ]]; then

			# Nullify automated PW
			G_CONFIG_INJECT 'AUTO_SETUP_GLOBAL_PASSWORD=' 'AUTO_SETUP_GLOBAL_PASSWORD=Password has been encrypted and secured on rootFS' /boot/dietpi.txt

			G_EXEC mkdir -p /var/lib/dietpi/dietpi-software # Should already exist, failsafe

			# Use new "pbkdf2" function on Buster to resolve warning about deprecated key derivation used: https://github.com/MichaIng/DietPi/issues/2213
			local pbkdf2=()
			(( $G_DISTRO > 4 )) && pbkdf2=('-iter' '10000')
			openssl enc -e -a -md sha256 -aes-256-cbc "${pbkdf2[@]}" -salt -pass pass:'DietPiRocks!' -out /var/lib/dietpi/dietpi-software/.GLOBAL_PW.bin <<< "$pw_dietpi_software"

			G_EXEC chown root:root /var/lib/dietpi/dietpi-software/.GLOBAL_PW.bin
			G_EXEC chmod 0600 /var/lib/dietpi/dietpi-software/.GLOBAL_PW.bin

			G_DIETPI-NOTIFY 2 'Global software password successfully changed'

		fi

		# Apply | root/dietpi users PW
		if [[ $pw_root_dietpi_users ]]; then

			chpasswd <<< "root:$pw_root_dietpi_users"
			chpasswd <<< "dietpi:$pw_root_dietpi_users"
			G_DIETPI-NOTIFY 2 '"root" and "dietpi" login passwords successfully changed'

		fi

	}

	#/////////////////////////////////////////////////////////////////////////////////////
	# Main Loop
	#/////////////////////////////////////////////////////////////////////////////////////
	#-----------------------------------------------------------------------------------
	# Info
	G_DIETPI-NOTIFY 3 "$G_PROGRAM_NAME" "$INPUT_MODE_NAME${INPUT_MODE_VALUE:+ ($INPUT_MODE_VALUE)}"
	#-----------------------------------------------------------------------------------
	if [[ $INPUT_MODE_NAME == 'apt-mirror' ]]; then

		AptMirror_Main

	elif [[ $INPUT_MODE_NAME == 'apt-cache' ]]; then

		AptCache_Main

	elif [[ $INPUT_MODE_NAME == 'useradd' ]]; then

		Useradd_Main

	elif [[ $INPUT_MODE_NAME == 'userdel' ]]; then

		Userdel_Main

	elif [[ $INPUT_MODE_NAME == 'ntpd-mode' ]]; then

		NtpdMode_Main

	elif [[ $INPUT_MODE_NAME == 'allo' ]]; then

		Allo_Main

	elif [[ $INPUT_MODE_NAME == 'verify_dietpi.txt' ]]; then

		Verify_DietPi_Txt

	elif [[ $INPUT_MODE_NAME == 'locale' ]]; then

		Locale_Main

	elif [[ $INPUT_MODE_NAME == 'passwords' ]]; then

		Passwords_Main

	else

		Unknown_Input_Name

	fi

	#-----------------------------------------------------------------------------------
	G_DIETPI-NOTIFY -1 $EXIT_CODE "$INPUT_MODE_NAME $INPUT_MODE_VALUE"
	#-----------------------------------------------------------------------------------
	exit $EXIT_CODE
	#-----------------------------------------------------------------------------------
}
